home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Prog / B-C / C Servant™.cpt / C Servant™.rsrc / TEXT_136_aText.txt < prev    next >
Encoding:
Text File  |  1992-02-24  |  2.2 KB  |  65 lines

  1.  
  2. 9. Else Clause; Conditional Expressions
  3.  
  4.             We just used an else after an if. The most general form of if is
  5.  
  6.             if (expression) statement1 else statement2
  7.  
  8. the else part is optional, but often useful. The canonical example sets x to the minimum of a and b:
  9.  
  10.             if (a < b)
  11.                         x = a;
  12.             else
  13.                         x = b;
  14.  
  15. Observe that there‚Äôs a semicolon after x=a.
  16.  
  17.             C provides an alternate form of conditional which is often more concise. It is called the ``conditional expression‚Äô‚Äô because it is a conditional which actually has a value and can be used anywhere an expression can. The value of
  18.  
  19.             a<b ? a : b;
  20.  
  21. is a if a is less than b; it is b otherwise. In general, the form
  22.  
  23.             expr1 ? expr2 : expr3
  24.  
  25. means ``evaluate expr1. If it is not zero, the value of the whole thing is expr2; otherwise the value is expr3.‚Äô‚Äô
  26.  
  27.             To set x to the minimum of a and b, then:
  28.  
  29.             x = (a<b ? a : b);
  30.  
  31. The parentheses aren‚Äôt necessary because `?:‚Äô is evaluated before `=‚Äô, but safety first.
  32.  
  33.             Going a step further, we could write the loop in the 
  34. lower-case program as
  35.  
  36.             while( (c=getchar( )) != ‚Äò\0‚Äô )
  37.                         putchar( (‚ÄòA‚Äô<=c && c<=‚ÄôZ‚Äô) ? c-‚ÄôA‚Äô+‚Äôa‚Äô : c );
  38.  
  39.             If‚Äôs and else‚Äôs can be used to construct logic that branches one of several ways and then rejoins, a common programming structure, in this way:
  40.  
  41.             if(...)
  42.                         {...}
  43.             else if(...)
  44.                         {...}
  45.             else if(...)
  46.                         {...}
  47.             else
  48.                         {...}
  49.  
  50. The conditions are tested in order, and exactly one block is executed ‚Äî either the first one whose if is satisfied, or the one for the last else. When this block is finished, the next statement executed is the one after the last else. If no action is to be taken for the ``default‚Äô‚Äô case, omit the last else.
  51.  
  52.             For example, to count letters, digits & others in a file, we could write
  53.  
  54.             main( ) {
  55.                         int let, dig, other, c;
  56.                         let = dig = other = 0;
  57.                         while( (c=getchar( )) != ‚Äò\0‚Äô )
  58.                                     if( (‚ÄòA‚Äô<=c && c<=‚ÄôZ‚Äô) || (‚Äòa‚Äô<=c && c<=‚Äôz‚Äô) ) ++let;
  59.                                     else if( ‚Äò0‚Äô<=c && c<=‚Äô9‚Äô ) ++dig;
  60.                                     else ++other;
  61.                         printf(‚Äú%d letters, %d digits, %d others\n‚Äù, let, dig, other);
  62.             }
  63.  
  64. The `++‚Äô operator means ``increment by 1‚Äô‚Äô; we will get to it in the next section.
  65.